home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 May / macformat-024.iso / Shareware City / Developers / nshellmegasource1.50 / mega src / commands / battery.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-23  |  2.4 KB  |  99 lines  |  [TEXT/KAHL]

  1. /* ========== the commmand file: ==========
  2.  
  3.     battery.c
  4.     
  5.     Copyright (c) 1993,1994 Newport Software Development
  6.     
  7.     You may distribute unmodified copies of this file for
  8.     noncommercial purposes.  You may use this file as a
  9.     reference when writing your own nShell(tm) commands.
  10.     
  11.     All other rights are reserved.
  12.     
  13.    ========== the commmand file: ========== */
  14.  
  15. #ifdef __MWERKS__            // CodeWarrior requires an A4 setup
  16. #include <A4Stuff.h>
  17. #endif
  18.  
  19. #include <GestaltEqu.h>
  20. #include <power.h>
  21.                     
  22. #include "nshc.h"
  23.  
  24. #include "nshc_utl.proto.h"
  25.                     
  26. void main(t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls)
  27. {
  28.     OSErr    error;
  29.     char    recently;
  30.     char    lovoltage;
  31.     char    highrate;
  32.     char    connected;
  33.     long    response;
  34.     Byte    status;
  35.     Byte    power;
  36.  
  37. #ifdef __MWERKS__
  38.     long oldA4  = SetCurrentA4();
  39. #endif
  40.     
  41.     if (nshc_bad_version( nshc_parms, nshc_calls, NSHC_VERSION )) goto Cleanup;
  42.     
  43.     nshc_parms->result = NSHC_ERR_GENERAL;        // preset return status to error
  44.     nshc_parms->action = nsh_idle;
  45.  
  46.     error = Gestalt( 'powr', &response );
  47.     
  48.     if (error) {
  49.         nshc_calls->NSH_printf_err("battery: The Gestalt call returned an error of %d.\r", error);
  50.         goto Cleanup; // error status is already set
  51.         }
  52.         
  53.     if ( !(response & ( 1 << gestaltPMgrExists ))) {
  54.         nshc_calls->NSH_putStr_err("\pbattery: The Power Manager is not installed.\r");
  55.         goto Cleanup; // error status is already set
  56.         }
  57.  
  58.     error = BatteryStatus( &status, &power );
  59.     
  60.     if (error) {
  61.         nshc_calls->NSH_printf_err("battery: The BatteryStatus call returned an error of %d.\r", error);
  62.         goto Cleanup; // error status is already set
  63.         }
  64.  
  65.     recently  = ( status & connChangedMask );
  66.     lovoltage = ( status & batteryLowMask );
  67.     highrate  = ( status & hiChargeMask );
  68.     connected = ( status & chargerConnMask );
  69.     
  70.     if (recently)
  71.         nshc_calls->NSH_puts("The charger has recently been");
  72.     else
  73.         nshc_calls->NSH_puts("The charger is");
  74.     
  75.     if (connected)
  76.         nshc_calls->NSH_puts(" connected.\r");
  77.     else
  78.         nshc_calls->NSH_puts(" disconnected.\r");
  79.     
  80.     if (lovoltage)
  81.         nshc_calls->NSH_puts("The low voltage flag is set.\r");        
  82.  
  83.     if (highrate)
  84.         nshc_calls->NSH_puts("The high charging rate flag is set.\r");
  85.     
  86.     nshc_calls->NSH_printf("Battery voltage is now %f V.\r", ((power/100.0)+5.12));
  87.  
  88.     nshc_parms->result = NSHC_NO_ERR;
  89.     nshc_parms->action = nsh_idle;
  90.     
  91. Cleanup:
  92.  
  93. #ifdef __MWERKS__
  94.     SetA4(oldA4);        // CodeWarrior needs to restore A4
  95. #else
  96.     ;                    // Think needs a null statement for the Cleanup label
  97. #endif
  98. }
  99.